﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace XNAStuding
{
    /// <summary>
    /// this class is used to drag and drop the textures(tools)in experiment 2 
    /// </summary>
    class dragPhysical:DrawableGameComponent
    {
    
        Vector2 mouseposition = new Vector2();
        Rectangle posD;
        Rectangle posTex;
        drawRot d;
        bool mouseOver;//check if the mouse pressed over the object or not
        bool movable;//check if the object can move or not 

        /// <summary>
        /// This class is The Class Constractor
        /// </summary>
        /// <param name="game"></param>
        /// <param name="d">object from class drawRot which represent the texture(tool) that we want to drag and drop</param>
        /// <param name="posD">the position of the distination rectangle in which the texture will be drew</param>
        /// <param name="posTex">the position of the source rectangle from which the texture will be drew</param>
        public dragPhysical(Game game,drawRot d,Rectangle posD,Rectangle posTex): base(game)
        {
            this.d = d;
            this.posTex = posTex;
            this.posD = posD;
            movable = true;
            
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
                MouseState mouse_state = Mouse.GetState();
                mouseposition.X = mouse_state.X;
                mouseposition.Y = mouse_state.Y;
                Rectangle mouseRec = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);
                //rectangele that contains the mouse 
                //if the two rectangels intersect each other 
                if (d.posD.Intersects(mouseRec) && mouse_state.LeftButton == ButtonState.Pressed && movable)
                {
                    mouseOver = true;
                }
                //if we pressed over the object 
                if (mouseOver)
                {
                    mouseOver = false;
                    d.posD.X = (int)mouseposition.X -(int)(44/2);
                    d.posD.Y = (int)mouseposition.Y -(int)(99/2);

                 }
                //if we reached the right postion 
                if (d.posD.X >= posD.X && d.posD.X <= posD.X + 300 && d.posD.Y >= posD.Y && d.posD.Y <= posD.Y + 300 && mouse_state.LeftButton == ButtonState.Released)
                {
                    //Get the large Texture from the Background
                    d.posTex = posTex;
                    d.posD.Width = posTex.Width;
                    d.posD.Height = posTex.Height;
                    //Put the Large texture in the right postion 
                    d.posD.X = posD.X;
                    d.posD.Y = posD.Y;
                    movable = false;
                }
            
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
                d.Draw(gameTime);
                base.Draw(gameTime);
        }
    }
}
   
